package com.ruoyi.business.controller;

import com.ruoyi.business.domain.Lab;
import com.ruoyi.business.domain.Rack;
import com.ruoyi.business.service.ILabService;
import com.ruoyi.business.service.IRackService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实验台Controller
 * 
 * @author qiancheng
 * @date 2025-06-13
 */
@Api(tags = "实验台管理")
@RestController
@RequestMapping("/business/lab")
public class LabController extends BaseController
{
    @Autowired
    private ILabService labService;

    @Autowired
    private IRackService rackService;

    /**
     * 实验平台管理->>实验台运行状态->>查询实验台列表
     */
    @ApiOperation("查询实验台列表")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam("实验台查询条件") Lab lab)
    {
        startPage();
        List<Lab> list = labService.selectLabList(lab);
        return getDataTable(list);
    }

    /**
     * 实验平台管理->>实验台运行状态->>根据实验台查询所属台架列表
     */
    @ApiOperation("根据实验台查询所属台架列表")
    @Anonymous
    @GetMapping("/getRackList")
    public TableDataInfo getRackList(@ApiParam("台架查询条件") Rack rack)
    {
        List<Rack> list = rackService.selectRackList(rack);
        return getDataTable(list);
    }

    /**
     * 导出实验台列表
     */
    @ApiOperation("导出实验台列表")
    @PreAuthorize("@ss.hasPermi('business:lab:export')")
    @Log(title = "实验台", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ApiParam("实验台查询条件") Lab lab)
    {
        List<Lab> list = labService.selectLabList(lab);
        ExcelUtil<Lab> util = new ExcelUtil<Lab>(Lab.class);
        util.exportExcel(response, list, "实验台数据");
    }

    /**
     * 获取实验台详细信息
     */
    @ApiOperation("获取实验台详细信息")
    @PreAuthorize("@ss.hasPermi('business:lab:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam("实验台ID") @PathVariable("id") Long id)
    {
        return success(labService.selectLabById(id));
    }

    /**
     * 新增实验台
     */
    @ApiOperation("新增实验台")
    @PreAuthorize("@ss.hasPermi('business:lab:add')")
    @Log(title = "实验台", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("实验台信息") @RequestBody Lab lab)
    {
        return toAjax(labService.insertLab(lab));
    }

    /**
     * 修改实验台
     */
    @ApiOperation("修改实验台")
    @PreAuthorize("@ss.hasPermi('business:lab:edit')")
    @Log(title = "实验台", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("实验台信息") @RequestBody Lab lab)
    {
        return toAjax(labService.updateLab(lab));
    }

    /**
     * 删除实验台
     */
    @ApiOperation("删除实验台")
    @PreAuthorize("@ss.hasPermi('business:lab:remove')")
    @Log(title = "实验台", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("实验台ID数组") @PathVariable Long[] ids)
    {
        return toAjax(labService.deleteLabByIds(ids));
    }
}
